/*
 * @Author: wangdazhuang
 * @Date: 2024-08-15 17:14:24
 * @LastEditTime: 2025-02-18 14:36:09
 * @LastEditors: wangdazhuang
 * @Description: 
 * @FilePath: /pornhub_app/lib/app.dart
 */

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/assets/styles.dart';
import 'package:pornhub_app/env/environment_service.dart';
import 'package:pornhub_app/routes/pages.dart';
import 'package:pornhub_app/routes/routes.dart';
import 'package:pornhub_app/services/user_service.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:pornhub_app/utils/logger.dart';
import 'package:pornhub_app/views/main/controllers/main_controller.dart';

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    final initialRoute = !Environment.isHaveNetWork
        ? Routes.no_net_work
        : Environment.isChooseSuccess
            ? Routes.launch
            : Routes.noSignal;
    const defaultTextStyle = TextStyle(color: COLOR.primaryText);
    return ScreenUtilInit(
      designSize: const Size(360, 690),
      child: GetMaterialApp(
        theme: ThemeData(
          primaryColor: Colors.black,
          scaffoldBackgroundColor: COLOR.scaffoldBg,
          textTheme: const TextTheme(
            displayLarge: defaultTextStyle,
            displayMedium: defaultTextStyle,
            displaySmall: defaultTextStyle,
            headlineLarge: defaultTextStyle,
            headlineMedium: defaultTextStyle,
            headlineSmall: defaultTextStyle,
            titleLarge: defaultTextStyle,
            titleMedium: defaultTextStyle,
            titleSmall: defaultTextStyle,
            bodyLarge: defaultTextStyle,
            bodyMedium: defaultTextStyle,
            bodySmall: defaultTextStyle,
            labelLarge: defaultTextStyle,
            labelMedium: defaultTextStyle,
            labelSmall: defaultTextStyle,
          ),
          appBarTheme: AppBarTheme(
            centerTitle: true,
            scrolledUnderElevation: 0.0,
            backgroundColor: COLOR.scaffoldBg,
            titleTextStyle: const TextStyle(
              fontSize: 17,
              color: COLOR.white,
              fontWeight: FontWeight.w500,
            ),
            elevation: 0,
            iconTheme: IconThemeData(color: COLOR.hexColor('#999999')),
            surfaceTintColor: COLOR.hexColor('#ffffff'),
            actionsIconTheme:
                IconThemeData(color: Styles.color.appBarIconColor),
            toolbarTextStyle: TextStyle(color: Styles.color.primaryText),
          ),
          sliderTheme: const SliderThemeData(
            trackHeight: 6,
            disabledActiveTrackColor: COLOR.color_393939,
            disabledInactiveTrackColor: COLOR.color_393939,
            disabledThumbColor: COLOR.white,
            thumbColor: COLOR.white,
            disabledSecondaryActiveTrackColor: COLOR.color_393939,
            inactiveTrackColor: COLOR.color_393939,
            activeTickMarkColor: COLOR.color_B93FFF,
            activeTrackColor: COLOR.color_B93FFF,
          ),
        ),
        initialRoute: initialRoute,
        getPages: Pages.pages,
        navigatorObservers: [CustomNavigatorObserver()],
        builder: (context, child) {
          return FlutterSmartDialog(child: child);
        },
      ),
    );
  }
}

class CustomNavigatorObserver extends NavigatorObserver {
  @override
  void didPop(Route route, Route? previousRoute) {
    MainController? mainvc;
    try {
      // web页面刷新会丢失MainController
      mainvc = Get.find<MainController>();
    } catch (e) {
      logger.i(
          'didPop route:${route.settings.name},prev:${previousRoute?.settings.name}, $e');
    }

    final index = mainvc?.currentIndex.value;
    if (index == MainController.mine &&
        previousRoute != null &&
        (previousRoute.settings.name ?? '').startsWith("/home/")) {
      Get.find<UserService>().updateAll();
    }
    super.didPop(route, previousRoute);
  }
}
